package pira.parser.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

public abstract class PageParser {
	
	public static final String RESULT= "<div class=\"searchresult\">";
	public static final String BLOCKHIGHLIGHT= "<div class=\"blockhighlight_box\">";
	public static final String PREFIX = "http://citeseerx.ist.psu.edu/";
	public static final String SEARCH = "search?q=";
	public static final String SHOWCITING = "showciting";
	public static final String SEARCHSUFFIX= "&submit=Search&sort=rel";
	public static final String PAPERPREFIX = "viewdoc/summary";
	public static final String CITELIST = "<table class=\"citelist\">";
	
	public static final String SORTSUFFIX= "&sort=cite&start=";
	
	public static final String INFORBAR = "<div class=\"information_bar char_increased\">";
	public static final String LEFTCONTENT = "<div class=\"left_content\">";
	public static final String HREF = "<a href=";
	public static final String AUTHORS= "<li class=\"author char6 padded\">";
	public static final String AUTHORBAR = "<div id=\"introduction\" class=\"clearfix\">";
	public static final String AUTHORBAR2 = "<div class=\"char_increased char_indented char_mediumvalue padded\">";
	public static final String DASH = "&#8212;";
	public static final String HYPHEN = " - ";
	public static final String NDASH = "&ndash;";
	
	protected String url;
	protected String page;
	protected int resultNum;
	
	public class Article {
		public String title;
		public String[] authors;
		public int year;
		
		public Article(String title, String[] authors) {
			this.title= title;
			this.authors= authors;
		}
		
		public Article(String title, String[] authors, int year) {
			this(title, authors);
			this.year = year;
		}
		
		public void print(PrintStream out) {
			out.println("Title : " + this.title);
			out.println("Authors : ");
			for (String author : authors)
				out.println(author);
			out.println();
		}
	}
	
	public PageParser() {
		this.resultNum= 0;
		this.page= null;
	}
	
	protected abstract String generateURL();
	protected abstract boolean parseResult();
	protected abstract void parse();
	
	protected int getResultNumber() {
		int index= -1;
		String newPage= page;
		
		if ((index = newPage.indexOf(INFORBAR)) != -1) {
			newPage= newPage.substring(index);
			if ((index = newPage.indexOf(LEFTCONTENT)) != -1) {
				newPage= newPage.substring(index);
				int endIndex= newPage.indexOf("</div>");
				String resultInfo= newPage.substring(0, endIndex);
				
				String str = resultInfo.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(  
		                "<[^>]*>", "");  
		        str = str.replaceAll("[(/>)<]", "");
		        str= str.trim().replaceAll(",", "");
		        resultNum= Integer.parseInt(str.split(" ")[0]);
//System.out.println(resultNum);
			} 
		} 
		return resultNum;
	}
	
	protected String readContent(String myUrl) throws Exception {
		String charset = "";
		HttpURLConnection HTTPURLConn = null;

		URL httpurl = new URL(myUrl);

		HTTPURLConn = (HttpURLConnection) httpurl.openConnection();
		HTTPURLConn.setRequestProperty("User-agent", "Mozilla/4.0");
		charset = HTTPURLConn.getContentType();
		
		while (charset == null) {
			Thread.sleep(30000);
			HTTPURLConn = (HttpURLConnection) httpurl.openConnection();
			HTTPURLConn.setRequestProperty("User-agent", "Mozilla/4.0");
			charset = HTTPURLConn.getContentType();
		}
		
		if (charset.indexOf("charset=") != -1)
			charset = charset.substring(charset.indexOf("charset=")
					+ "charset=".length(), charset.length());
		else {
			System.out.println("charset2:" + charset);
		}
		if (charset == null) {
			System.out.println("charset31:" + charset);

		}

		InputStream is = HTTPURLConn.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is,
				"utf-8"));
		
		page= null;
		String line = null;
		
		while ((line= br.readLine()) != null) {
			page+= line;
//System.out.println(line);
		}
		
		return charset;
	}

	protected String getTitle(String line) {
		int beginIndex, endIndex;
		
		if ((beginIndex= line.indexOf("<a class=\"remove doc_details\"")) != -1) {
			line= line.substring(beginIndex);
			beginIndex= 0;
			endIndex= line.indexOf("</a>");
			line= line.substring(beginIndex, endIndex);
			
			String str = line.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(  
	                "<[^>]*>", "");  
	        str = str.replaceAll("[(/>)<]", "");  
			return str.trim();
		} else
			return null;
	}
	
	protected String[] getAuthors(String line) {
		int beginIndex, endIndex;
		
		if ((beginIndex= line.indexOf(AUTHORS)) != -1) {
			line= line.substring(beginIndex);
			beginIndex= 0;
			endIndex= line.indexOf("</li>");
			line= line.substring(beginIndex, endIndex);
			
			String str = line.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(  
	                "<[^>]*>", "");  
	        str = str.replaceAll("[(/>)<]", "");  
	        
	        str = str.split(DASH)[0];
	        String[] names= str.split(",");
	        for (int i= 0; i < names.length; i++) {
	        	if (i == 0) {
	        		if (names[i].substring(0, 3).equals("by "))
	        			names[i]= names[i].substring(3);
	        	}
	        	
//	        	if (i == names.length-1 ) {
//	        		names[i]= names[i].split(DASH)[0];
//	        	} 
	        	
        		names[i] = names[i].trim();
	        }
			return names;
		} else
			return null;
	}
}
